﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class operate_operateQustionInfo : System.Web.UI.Page
{
    string[] keys ;
    protected void Page_Load(object sender, EventArgs e)
    {

        keys = Request.QueryString.AllKeys;
        //规范：1.operate 2.table 3.id

        if (Request.QueryString[keys[0]].ToString().Trim() == "del")
        {
            //删除题库
            DelQuestionInfo();

        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "add")
        {
            //添加题目
            AddQuestionInfo();
        }
        else if (Request.QueryString[keys[0]].ToString().Trim() == "update")
        {
            //修改题目
            UpdateQuestionInfo();

        }
        
            Response.End();
    }


    public void DelQuestionInfo()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_radio_questions")
        {
            string radio_question_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_radio_questions  where radio_question_id ='" + radio_question_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        //多选
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_mutil_questions")
        {
            string mutil_question_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_mutil_questions  where mutil_question_id ='" + mutil_question_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
       //判断
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_judge_questions")
        {
            string judge_question_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_judge_questions  where judge_question_id ='" + judge_question_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
        //解答
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_answer_questions")
        {
            string answer_question_id = Request.QueryString[keys[2]];
            string sql = "delete from tb_answer_questions  where answer_question_id ='" + answer_question_id + "'";
            int i = SqlServer.ExecuteNonQuery(sql);
            if (i > 0)
            {
                Response.Write(i.ToString());
            }
            else
            {
                Response.Write("0");//删除失败
            }
        }
    }
    public void UpdateQuestionInfo()
    {
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_radio_questions")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_radio_questions set " + s
                + " where radio_question_id = '" + Request.QueryString[keys[2]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//修改失败
            }
        }
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_mutil_questions")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_mutil_questions set " + s
                + " where mutil_question_id = '" + Request.QueryString[keys[2]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//修改失败
            }
        }
        #endregion
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_judge_questions")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_judge_questions set " + s
                + " where judge_question_id = '" + Request.QueryString[keys[2]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//修改失败
            }
        }
        #endregion
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_answer_questions")
        {
            List<string> set = new List<string>();
            for (int i = 3; i < keys.Length; i++)
            {
                set.Add(String.Format(" {0} = '{1}' ", keys[i], Request.QueryString[keys[i]]));
            }
            string s = string.Join(" , ", set);
            string sql = "update  tb_answer_questions set " + s
                + " where answer_question_id = '" + Request.QueryString[keys[2]] + "'";
            int k = SqlServer.ExecuteNonQuery(sql);
            if (k > 0)
            {
                Response.Write(k.ToString());
            }
            else
            {
                Response.Write("0");//修改失败
            }
        }
        #endregion
    }
    public void AddQuestionInfo()
    {
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_radio_questions")
        {
            string sql = "INSERT INTO [dbo].[tb_radio_questions]" +
                    "           ([radio_question_title]" +
                    "           ,[option_a]" +
                    "           ,[option_b]" +
                    "           ,[option_c]" +
                    "           ,[option_d]" +
                    "           ,[course_id]" +
                    "           ,[correct_answer])" +
                    "     VALUES("
                    + String.Format("'{0}',", Request.QueryString[keys[2]])
                    + String.Format("'{0}',", Request.QueryString[keys[3]])
                    + String.Format("'{0}',", Request.QueryString[keys[4]])
                    + String.Format("'{0}',", Request.QueryString[keys[5]])
                    + String.Format("'{0}',", Request.QueryString[keys[6]])
                    + String.Format("'{0}',", Request.QueryString[keys[8]])
                    + String.Format("'{0}'", Request.QueryString[keys[7]]) + ")";
            int i = SqlServer.ExecuteNonQuery(sql);
            Response.Write(i.ToString());
        }
        #endregion
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_mutil_questions")
        {
            string sql = "INSERT INTO [dbo].[tb_mutil_questions]" +
                    "           ([mutil_question_title]" +
                    "           ,[option_a]" +
                    "           ,[option_b]" +
                    "           ,[option_c]" +
                    "           ,[option_d]" +
                    "           ,[course_id]" +
                    "           ,[correct_answer])" +
                    "     VALUES("
                    + String.Format("'{0}',", Request.QueryString[keys[2]])
                    + String.Format("'{0}',", Request.QueryString[keys[3]])
                    + String.Format("'{0}',", Request.QueryString[keys[4]])
                    + String.Format("'{0}',", Request.QueryString[keys[5]])
                    + String.Format("'{0}',", Request.QueryString[keys[6]])
                    + String.Format("'{0}',", Request.QueryString[keys[8]])
                    + String.Format("'{0}'", Request.QueryString[keys[7]]) + ")";
            int i = SqlServer.ExecuteNonQuery(sql);
            Response.Write(i.ToString());
        }
        #endregion
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_judge_questions")
        {
            string sql = "INSERT INTO [dbo].[tb_judge_questions]" +
                    "           ([judge_question_title]" +
                    "           ,[course_id]" +
                    "           ,[correct_answer])" +
                    "     VALUES("
                    + String.Format("'{0}',", Request.QueryString[keys[2]])
                    + String.Format("'{0}',", Request.QueryString[keys[3]])
                    + String.Format("'{0}')", Request.QueryString[keys[4]]);
            int i = SqlServer.ExecuteNonQuery(sql);
            Response.Write(i.ToString());
        }
        #endregion
        #region
        if (Request.QueryString[keys[1]].ToString().Trim() == "tb_answer_questions")
        {
            string sql = "INSERT INTO [dbo].[tb_answer_questions]" +
                    "           ([answer_question_title]" +
                    "           ,[course_id]" +
                    "           ,[correct_answer])" +
                    "     VALUES("
                    + String.Format("'{0}',", Request.QueryString[keys[2]])
                    + String.Format("'{0}',", Request.QueryString[keys[3]])
                    + String.Format("'{0}')", Request.QueryString[keys[4]]);
            int i = SqlServer.ExecuteNonQuery(sql);
            Response.Write(i.ToString());
        }
        #endregion
    }
}